#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
from PyQt4.Qt import *
from PyQt4 import QtCore,QtGui
from xml.dom import minidom
from versewindow_ui import Ui_versewindow
from versechooser_ui import Ui_versechooser
bible = minidom.parse('biblia.xml')


class MyPopup(QWidget):
    def __init__(self):
        QWidget.__init__(self)
	self.ui=Ui_versewindow()
	self.ui.setupUi(self)

class MainWindow(QMainWindow):
    def __init__(self, *args):
        QMainWindow.__init__(self, *args)
	self.ui=Ui_versechooser()
	self.ui.setupUi(self)
	books_list = open('id_livros.txt')
	count=1
	for item in books_list.readlines():
		self.ui.cb_book.insertItem(count, item.strip("\n").encode('latin-1'))
		count=count+1
        
	self.connect(self.ui.bt_project, SIGNAL("clicked()"), self.showFull)
	self.connect(self.ui.bt_preview, SIGNAL("clicked()"), self.preview)
	self.connect(self.ui.bt_previous, SIGNAL("clicked()"), self.previousVerse)
	self.connect(self.ui.bt_next, SIGNAL("clicked()"), self.nextVerse)

        self.w = None

    def showFull(self):
	if self.ui.bt_project.text() == "Projetar":
		book 	  = self.ui.cb_book.currentIndex()+1
		cap       = self.ui.sb_cap.value()
		vers      = self.ui.sb_vers.value()
		self.ui.bt_project.setText("Ocultar")

		self.w = MyPopup()
		#TODO: Don't set fullscreen if one screen. Offer fullscreen shortcut		
		if self.ui.actionPrim_ria.isChecked():
			self.w.setGeometry(QRect(0, 0, 600, 600))
		else:
			sw = QtGui.QDesktopWidget().screenGeometry().width()
			sh = QtGui.QDesktopWidget().screenGeometry().height()
			self.w.setGeometry(QRect(sw+1,sh+1, 600, 600))
		verse_text = self.getVerse(book,cap,vers)
	
		bookName = bible.firstChild.childNodes[(book*2)-1].attributes["bname"].value
		verse_title = bookName.encode('latin-1')+" "+str(cap)+":"+str(vers)
		self.w.ui.lb_verse_title.setText(str(verse_title))
		
		#TODO: Set text size according screen resolution and string length
		self.w.ui.lb_verse.setText(str(verse_text))
		self.w.showFullScreen()
	else:
		self.ui.bt_project.setText("Projetar")		
		self.hidescreen()

    def previousVerse(self):
	self.ui.sb_vers.stepDown()
	book 	  = self.ui.cb_book.currentIndex()+1
	cap       = self.ui.sb_cap.value()
	vers      = self.ui.sb_vers.value()
	verse_text = self.getVerse(book,cap,vers)
	self.w.ui.lb_verse.setText(str(verse_text))
	bookName = bible.firstChild.childNodes[(book*2)-1].attributes["bname"].value
	verse_title = bookName.encode('latin-1')+" "+str(cap)+":"+str(vers)
	self.w.ui.lb_verse_title.setText(str(verse_title))

    def nextVerse(self):
	#TODO: Exceptions
	self.ui.sb_vers.stepUp()
	book 	  = self.ui.cb_book.currentIndex()+1
	cap       = self.ui.sb_cap.value()
	vers      = self.ui.sb_vers.value()
	verse_text = self.getVerse(book,cap,vers)
	self.w.ui.lb_verse.setText(str(verse_text))
	bookName = bible.firstChild.childNodes[(book*2)-1].attributes["bname"].value
	verse_title = bookName.encode('latin-1')+" "+str(cap)+":"+str(vers)
	self.w.ui.lb_verse_title.setText(str(verse_title))

    def preview(self):
	#TODO: Exceptions
	book 	  = self.ui.cb_book.currentIndex()+1
	cap       = self.ui.sb_cap.value()
	vers      = self.ui.sb_vers.value()
	verse_text = self.getVerse(book,cap,vers)
	self.ui.lb_preview.setText(str(verse_text))

    def getVerse(self,book,cap,vers):
	#TODO: Exceptions
	bible_books = bible.firstChild
	verse_text = bible_books.childNodes[(book*2)-1].childNodes[(cap*2)-1].childNodes[(vers*2)-1].firstChild.data.encode('latin-1')
	return verse_text

    def hidescreen(self):
	self.w.close()

class App(QApplication):
    def __init__(self, *args):
        QApplication.__init__(self, *args)
        self.main = MainWindow()
        self.connect(self, SIGNAL("lastWindowClosed()"), self.finish )
        self.main.show()

    def finish( self ):
	#TODO Close popups before close main window
        self.exit(0)

def main(args):
    #TODO: Progress bar on load
    global app
    app = App(args)
    app.exec_()

if __name__ == "__main__":
    main(sys.argv)

